<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>总结Vue数据监测</title>
    <script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
    <div id="app">
        <h1>学生信息</h1> <br/>
        <button @click="student.age++">年龄+1</button>
        <button @click="addSex">添加性别属性，默认值为：男</button>
        <button @click="addFriendAtFirst">在列表首位添加一个朋友</button>
        <button @click="updateFirstFriendName">修改第一个朋友的名字为：张三</button>
        <button @click="addHobby">添加一个爱好</button>
        <button @click="updateFirstHobby">修改第一个爱好为：学习</button>

        <h3>姓名：{{student.name}}</h3>
        <h3>年龄：{{student.age}}</h3>
        <h3 v-if="student.sex">性别：{{student.sex}}</h3>
        <h3>爱好：</h3>
        <ul>
            <li v-for="(h,index) in student.hobby" :key="index">
                {{h}}
            </li>
        </ul>
        <h3>朋友们：</h3>
        <ul v-for="(f,index) in student.friends" :key="index">
            {{f.name}}--{{f.age}}
        </ul>
    </div>

    <script type="text/javascript">
        const vm = new Vue({
            el: '#app',
            data: {
                student: {
                    name: 'Tom',
                    age: 18,
                    hobby: ['抽烟', '喝酒', '烫头'],
                    friends: [
                        {name: 'Jack', age: 22},
                        {name: 'Jerry', age: 21}
                    ]
                }
            },
            methods: {
                addSex() {
                    Vue.set(this.student, 'sex', '男'); // vm.$set => this.$set
                },
                addFriendAtFirst() {
                    this.student.friends.unshift({name: 'Luci', age: 26});
                },
                updateFirstFriendName() {
                    this.student.friends[0].name = '张三';
                },
                addHobby() {
                    this.student.hobby.push('学习')
                },
                updateFirstHobby(){
                    this.student.hobby.splice(0,1,'学习'); // Vue.set(this.student.hobby,0,'学习')
                }
            }
        })
    </script>
</body>
</html>